package org.chench.test.hiserviceclient.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HiClientService {

    @Autowired
    private RestTemplate restTemplate;

    // 使用注解对方法创建断路器功能
    @HystrixCommand(fallbackMethod = "hiError")
    public String sayHi(String name) {
        String url = new StringBuilder().append("http://HI-SERVICE/hi/").append(name).toString();
        return restTemplate.getForObject(url, String.class);
    }

    public String hiError(String name) {
        return new StringBuilder().append("Hi, ").append(name).append(", Sorry! ERROR At ").append(this.getClass().getName()).toString();
    }
}
